import { decode, encode } from "cbor-x";
import { serverFetch, serverUpload } from "./http"

export interface TbFirm {
    id: number,
    version: string,
    description: string,
    size: number,
    name: number,
    create_timestamp: number,
}

export interface CreateFirmReq {
    firm_type: number,
    version: string,
    description: string,
    name: string,
    body: Uint8Array,
}


export default {

    async query(firmType: number) {
        const resp = await serverFetch(`/firm/query_all?firm_type=${firmType}`, 'GET');
        return decode(resp.body) as TbFirm[];
    },

    async delete(id: number) {
        await serverFetch('/firm/delete', 'POST', encode({ id }));
    },

    async create(req: CreateFirmReq, cb: (prog: string) => void) {
        await serverUpload('/firm/create', encode(req), cb)
    }
}

